import React from 'react';
import { Modal, DatePicker,Form } from 'antd';
import moment from 'moment';

const Item = Form.Item;
const { RangePicker } = DatePicker;

@Form.create()
class Components extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
  }
  /**
   * 提交表单到父组件
   */
  onOk = () => {
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        let params = {
          startTime: moment(values.date[0]).format('YYYY-MM-DD HH:mm:ss'),
          endTime: moment(values.date[1]).format('YYYY-MM-DD HH:mm:ss')
        }
        this.props.submit(params);
      }
    });
  };
  clear = () => {
    this.props.form.resetFields();
  };

  render() {
    const {
      visible,
      close,
      form: { getFieldDecorator },
    } = this.props;

    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 12 },
      },
    };
    return (
      <Modal
        title="选择时间"
        afterClose={this.clear}
        visible={visible}
        onCancel={close}
        okText="确定"
        cancelText="取消"
        onOk={this.onOk}
      >
        <Form>
          <Item {...formItemLayout} label="时间间隔">
            {getFieldDecorator('date', {
              rules: [{ required: true, message: '不能为空' }],
            })(<RangePicker />)}
          </Item>
        </Form>
      </Modal>
    );
  }
}

export default Components;
